package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import com.example.myapplication.toastutils.ToastUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;

public class registerPageAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    Button registerSuccess;
    TextField newUsername;
    TextField InputPassword;
    TextField InputPassword_2;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_register_ability_main1);
        registerSuccess = (Button) findComponentById(ResourceTable.Id_registerSuccess);
        newUsername = (TextField) findComponentById(ResourceTable.Id_newUsername);
        InputPassword = (TextField) findComponentById(ResourceTable.Id_InputPassword);
        InputPassword_2 = (TextField) findComponentById(ResourceTable.Id_InputPassword_2);

        registerSuccess.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        String newUsernameInput = newUsername.getText();
        String InputPassword_0 = InputPassword.getText();
        String InputPassword_2_0 = InputPassword_2.getText();
        if(component == registerSuccess) {
            if ("".equals(newUsernameInput)) {
                ToastUtils.showDialog(this, "用户名不能为空");
            }
            if ("".equals(InputPassword_0)) {
                ToastUtils.showDialog(this, "请输入您的密码");
            }
            if ((InputPassword_0.equals(InputPassword_2_0) && !newUsernameInput.isEmpty())) {
                ToastUtils.showDialog(this, "注册成功");
                ToastUtils.showDialog(this, "请重新登入");
                //跳转到哪一个页面中（意图）
                Intent A = new Intent();
                //意图中包含了要跳转的页面信息
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")//要跳转到哪个设备上，如果传递一个没有内容的字符串，表示跳转本机
                        .withBundleName("com.example.myapplication")//要跳转到哪个应用上，小括号里面可以写包名
                        .withAbilityName("com.example.myapplication.MainAbility")//要跳转的页面
                        .build();//将上面的三个信息进行打包到一个operation对象当中（点击build，按ctrl+Alt+v）
                A.setOperation(operation);//把打包之后的operation设置到意图里
                startAbility(A);//跳转页面
            }
            if(!InputPassword_0.equals(InputPassword_2_0)){
                ToastUtils.showDialog(this, "两次输入的密码应一致");
            }
        }
    }
}
